package com.yuan.yblog.portal.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yuan.yblog.common.api.CommonResult;
import com.yuan.yblog.portal.aop.annotation.Log;
import com.yuan.yblog.portal.aop.annotation.SlidingWindowLimiter;
import com.yuan.yblog.portal.enums.BusinessType;
import com.yuan.yblog.portal.enums.LimitType;
import com.yuan.yblog.portal.pojo.VO.CommentVO;
import com.yuan.yblog.portal.service.CommentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * (Comment)表控制层
 * @since 2023-12-03 14:21:17
 */
@RestController
@RequestMapping("/comment")
@Tag(name = "文章评论（comment）接口")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @SlidingWindowLimiter(key = "comment", windowSize = 3, windowTimeRange = 3, limitType = LimitType.IP)
    @Operation(summary = "根据文章id，分页获取评论")
    @GetMapping("/listByPostId")
    public CommonResult<IPage<CommentVO>> list(
            @Parameter(description = "文章id")  @RequestParam(defaultValue = "1") Long postId,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Long pageNumber,
            @Parameter(description = "页大小") @RequestParam(defaultValue = "7") Long pageSize,
            @Parameter(description = "排序字段") @RequestParam(required = false) String sortKey,
            @Parameter(description = "是否降序") @RequestParam(required = false) Boolean isDesc){
        IPage<CommentVO> result = commentService.listByPostId(postId, pageNumber, pageSize, sortKey, isDesc);
        return CommonResult.success(result);
    }

    @Operation(summary = "分页获取指定用户评论信息")
    @GetMapping("/listByUserId")
    public CommonResult<IPage<CommentVO>> listByUserId(
            @Parameter(description = "用户id")  @RequestParam Long userId,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Long pageNumber,
            @Parameter(description = "页大小") @RequestParam(defaultValue = "7") Long pageSize,
            @Parameter(description = "排序字段") @RequestParam(required = false) String sortKey,
            @Parameter(description = "是否降序") @RequestParam(required = false) Boolean isDesc){
        IPage<CommentVO> res = commentService.listByUserId(userId, pageNumber, pageSize, sortKey, isDesc);
        return  CommonResult.success(res);
    }

    @SlidingWindowLimiter(key = "comment", windowSize = 3, windowTimeRange = 3, limitType = LimitType.IP)
    @Operation(summary = "根据评论id，查询评论详情")
    @GetMapping("/details/{id}")
    public CommonResult<CommentVO> selectOne(@Parameter(description = "评论") @PathVariable("id") Long commentId) {
        CommentVO comment = commentService.selectOne(commentId);
        if(comment == null) {
            return CommonResult.failed("查询失败");
        }
        return  CommonResult.success(comment);
    }

    @Log(title = "新增评论", businessType = BusinessType.INSERT)
    @Operation(summary = "插入新评论")
    @PostMapping("/insert")
    public CommonResult<Long> insert(@Parameter(description = "评论提交实体") @RequestBody CommentVO commentVO) {
        commentVO.setId(null);
        Long id = commentService.insert(commentVO);
        if (id == null) {
            return CommonResult.failed("插入失败");
        }
        return CommonResult.success(commentVO.getId(),"插入成功，id=" + id);
    }

    @Log(title = "修改评论", businessType = BusinessType.UPDATE)
    @Operation(summary = "修改评论")
    @PostMapping("/update")
    public CommonResult<Long> update(@Parameter(description = "评论提交实体") @RequestBody CommentVO commentVO) {
        Long id = commentService.update(commentVO);
        if (id == null) {
            return CommonResult.failed("评论失败");
        }
        return CommonResult.success(id,"更新成功，id=" + id);
    }


    @Log(title = "删除评论", businessType = BusinessType.DELETE)
    @Operation(summary = "删除评论，根据id，逻辑删除")
    @DeleteMapping("delete")
    public CommonResult<Long> delete(@Parameter(description = "评论id") @RequestParam("commentId") Long commentId) {
        Long id = commentService.delete(commentId);
        if (id != null) {
            return CommonResult.success(id,"删除成功，删除id="+id);

        }
        return CommonResult.failed("删除失败");
    }
}

